using UnityEditor;
using UnityEngine;

namespace GameMain.Editor.BuildPipeline
{
    public partial class BuildPipeline
    {
        private static string HybridClrBuildCacheDir => Application.dataPath + "/HybridCLRBuildCache";
        private static string AssetBundleOutputDir => $"{HybridClrBuildCacheDir}/AssetBundleOutput";
        private static string AssetBundleSourceDataTempDir => $"{HybridClrBuildCacheDir}/AssetBundleSourceData";

        private static string GetAssetBundleOutputDirByTarget(BuildTarget target)
        {
            return $"{AssetBundleOutputDir}/{target}";
        }

        private static string GetAssetBundleTempDirByTarget(BuildTarget target)
        {
            return $"{AssetBundleSourceDataTempDir}/{target}";
        }

        private static string ToRelativeAssetPath(string s)
        {
            return s.Substring(s.IndexOf("Assets/"));
        }

        /// <summary>
        /// 将HotFix.dll和HotUpdatePrefab.prefab打入common包.
        /// 将HotUpdateScene.unity打入scene包.
        /// </summary>
        /// <param name="tempDir"></param>
        /// <param name="outputDir"></param>
        /// <param name="target"></param>
        private static void BuildAssetBundles(string tempDir, string outputDir, BuildTarget target)
        {
            /*
            Directory.CreateDirectory(tempDir);
            Directory.CreateDirectory(outputDir);

            var abs = new List<AssetBundleBuild>();
            
            var luaFiles = Directory.GetFiles(Application.dataPath + "/Res/lua/", "*.txt", SearchOption.AllDirectories);
            if (luaFiles.Length > 0)
            {
                abs.Add(new AssetBundleBuild
                {
                    assetBundleName = "lua",
                    assetNames = luaFiles.Select(s => ToRelativeAssetPath(s)).ToArray(),
                });
            }

            UnityEditor.BuildPipeline.BuildAssetBundles(outputDir, abs.ToArray(),
                BuildAssetBundleOptions.DeterministicAssetBundle
                | BuildAssetBundleOptions.ChunkBasedCompression |
                BuildAssetBundleOptions.DisableLoadAssetByFileNameWithExtension, target);

            AssetDatabase.Refresh();

            var streamingAssetPathDst = $"{Application.streamingAssetsPath}";
            Directory.CreateDirectory(streamingAssetPathDst);

            foreach (var ab in abs)
            {
                var from = ToRelativeAssetPath($"{outputDir}/{ab.assetBundleName}");
                var to = ToRelativeAssetPath($"{streamingAssetPathDst}/{ab.assetBundleName}");

                Debug.Log($"copy to streamingassets:{from}-{to}");
                AssetDatabase.CopyAsset(from, to);
            }*/
        }

        private static void BuildAssetBundleByTarget(BuildTarget target)
        {
            var tmpDir = GetAssetBundleTempDirByTarget(target);
            var outDir = GetAssetBundleOutputDirByTarget(target);
            BuildAssetBundles(tmpDir, outDir, target);
        }

   //     [MenuItem("HybridCLR/BuildBundles/ActiveBuildTarget")]
        private static void BuildSceneAssetBundleActiveBuildTarget()
        {
            BuildAssetBundleByTarget(EditorUserBuildSettings.activeBuildTarget);
        }

    //    [MenuItem("HybridCLR/BuildBundles/Win64")]
        private static void BuildSceneAssetBundleWin64()
        {
            BuildAssetBundleByTarget(BuildTarget.StandaloneWindows64);
        }

      //  [MenuItem("HybridCLR/BuildBundles/Android")]
        private static void BuildSceneAssetBundleAndroid()
        {
            BuildAssetBundleByTarget(BuildTarget.Android);
        }

    //    [MenuItem("HybridCLR/BuildBundles/IOS")]
        private static void BuildSceneAssetBundleIos()
        {
            BuildAssetBundleByTarget(BuildTarget.iOS);
        }
    }
}
